<?php
namespace app\shell;


use share\console\CConsole;
use share\console\CLIRunner;
use app\model\DeviceBaseModel;
use app\model\DeviceInfoTableModel;
use app\model\SimBindDeviceModel;
use app\model\UserDeviceBindingModel;
use app\model\UserDeviceShareModel;
use app\service\TableDataChangeService;
use share\model\DeviceRegisterTableModel;
use share\model\LicenseV2Model;
use share\model\LicenseModel;
use share\model\UserInfoModel;
use share\model\UserPassModel;
use share\utils\Helper;

class CTableDataChange extends CLIRunner{
// 现有超过1W条数据的表
// enz_aliyun	device_base	942887
// enz_aliyun	user_info	163983
// enz_aliyun	user_pass	148225
// enz_aliyun	device_info_table	86593
// enz_aliyun	user_device_binding	85795
// enz_aliyun	user_device_share	42247
// enz_aliyun	sim_bind_device	19473

// burn_secret_db	hdt_nvr_license_v2	181490
// burn_secret_db	hdt_ipcam_license_v2	157349
// burn_secret_db	hdt_nvr_license	125650
// burn_secret_db	device_register_table	64018
// burn_secret_db	enz_ipcam_license_v2	36518
// burn_secret_db	hdt_ipcam_license	34633
// burn_secret_db	fslink_ipcam_license_v2	18618
// burn_secret_db	enz_ipcam_license	14620
// burn_secret_db	fslink_ipcam_license	12282
// burn_secret_db	tungson_ipcam_license_v2	10129

    public function recount_current_day_action(){
        $startTime = strtotime(date("Y-m-d"));
        $day = intval(date("Ymd"));
        $models = self::model_info();
        foreach($models as $model){
            $db = $model->getDB();
            $table = $model->getTableName();
            $res = $model->field('count(*) as num')->where($model->createTimeField,'>=',$startTime)->find();
            $data = $model::formatModelData($res);
            $num = $data['num'];
            TableDataChangeService::addOrUpdate($db,$table,$day,$num);            
        }
    } 

    public function recount_last_week_action(){ 
        $tn = time();
        $startTime = strtotime(date("Y-m-d")." -7 days");
        $models = self::model_info();
        foreach($models as $model){
            $db = $model->getDB();
            $table = $model->getTableName();
            for($start = $startTime;$start < $tn;$start += 86400){
                $day = intval(date("Ymd",$start));
                $res = $model->field('count(*) as num')->where($model->createTimeField,'between',[$start,$start + 86400])->find();
                $data = $model::formatModelData($res);
                $num = $data['num'];
                TableDataChangeService::addOrUpdate($db,$table,$day,$num);
            }
        }
    }

    public function recount_last_month_action(){ 
        $tn = time();
        $startTime = strtotime(date("Y-m-d")." -30 days");
        $models = self::model_info();
        foreach($models as $model){
            $db = $model->getDB();
            $table = $model->getTableName();
            for($start = $startTime;$start < $tn;$start += 86400){
                $day = intval(date("Ymd",$start));
                $res = $model->field('count(*) as num')->where($model->createTimeField,'between',[$start,$start + 86400])->find();
                $data = $model::formatModelData($res);
                $num = $data['num'];
                TableDataChangeService::addOrUpdate($db,$table,$day,$num);
            }
        }
    }

    public static function model_info(){
        $hdt_nvr_license_v2 = new LicenseV2Model;
        $hdt_nvr_license_v2->setBase('hdt','nvr');

        $hdt_ipcam_license_v2 = new LicenseV2Model;
        $hdt_ipcam_license_v2->setBase('hdt','ipcam');

        $hdt_nvr_license = new LicenseModel;
        $hdt_nvr_license->setBase('hdt','nvr');

        $enz_ipcam_license_v2 = new LicenseV2Model;
        $enz_ipcam_license_v2->setBase('enz','ipcam');

        $hdt_ipcam_license = new LicenseModel;
        $hdt_ipcam_license->setBase('hdt','ipcam');

        $fslink_ipcam_license_v2 = new LicenseV2Model;
        $fslink_ipcam_license_v2->setBase('fslink','ipcam');

        $enz_ipcam_license = new LicenseModel;
        $enz_ipcam_license->setBase('enz','ipcam');

        $fslink_ipcam_license = new LicenseModel;
        $fslink_ipcam_license->setBase('fslink','ipcam');

        $tungson_ipcam_license_v2 = new LicenseV2Model;
        $tungson_ipcam_license_v2->setBase('tungson','ipcam');

        return [
            new DeviceBaseModel,   
            new UserPassModel,
            new DeviceInfoTableModel,
            new UserDeviceBindingModel,
            new UserDeviceShareModel,
            new DeviceRegisterTableModel,
            $hdt_nvr_license_v2,
            $hdt_ipcam_license_v2,
            $hdt_nvr_license,
            $enz_ipcam_license_v2,
            $hdt_ipcam_license,
            $fslink_ipcam_license_v2,
            $enz_ipcam_license,
            $tungson_ipcam_license_v2,
        ];

    }
}